package com.yang.servlet;

import com.yang.service.RoleService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/RoleUpdateServlet")
public class RoleUpdateServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        RoleService roleService = new RoleService();
        String roleName = request.getParameter("roleName");
        String roleDesc = request.getParameter("roleDesc");
        int roleId = Integer.parseInt(request.getParameter("roleId"));
        String[] menuIds = request.getParameterValues("menuId");

        try {
            boolean b = roleService.updateRole(roleName, roleDesc, roleId, menuIds);
            if(b == true){
                request.setAttribute("tips","<label style='color:green'>修改成功</label>");
            }else {
                request.setAttribute("tips","<label style='color:red'>修改失败</label>");
            }
            request.getRequestDispatcher("prompt.jsp").forward(request,response);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
